/*
 * Created by Jaren at 2022/01/29 16:52
 */
import 'package:ble_plugin/ble_plugin.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jieli_ble_ota_plugin_example/base/base.dart';
import 'package:jieli_ble_ota_plugin_example/ota/fw_update_controller.dart';

class FwUpdatePage extends BaseView<FwUpdateController> {
  _buildActionViews(FwUpdateController controller) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 20, bottom: 20),
              child: MaterialButton(
                onPressed: () => {controller.startUpdate()},
                child: Obx(() => Text(controller.btnText.value)),
              ),
            )
          ],
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [Obx(() => Text(controller.updateStatus.value))],
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return WillPopScope(
        child: Scaffold(
            appBar: AppBar(
              title: const Text("FW Update"),
            ),
            body: Column(
              children: [
                Expanded(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      _buildActionViews(controller),
                    ],
                  ),
                ),
              ],
            )),
        onWillPop: () => _onBackPressed(controller));
  }

  Future<bool> _onBackPressed(FwUpdateController controller) {
    BleManager().disconnect();
    controller.release();
    return Future.value(true);
  }
}
